<?php
/**
 * This file is part of won-uploader.
 *
 * Author: Daniel K.
 *
 * won-uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * won-uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with won-uploader. If not, see <http://www.gnu.org/licenses/>.
 */

	if( !defined("won-uploader")) die();    // Silence is golden
    
    class NanoQuery {
        private $user;  // Username
        private $pass;  // Password
        private $db;    // Database
        private $host;  // Host
        
        private $link;  // Current MySQL connection
        private $res;   // Query resource
        
        private $error; // MySQL error
        
        /**
         * Connect to host and select database
         * 
         * @param   $username   Username
         * @param   $password   User password
         * @param   $database   Database to select
         * @param   $host       Database host
         */
        public function __construct($username,$passwd,$database,$host="localhost") {
            $this->user = $username;
            $this->pass = $passwd;
            $this->db   = $database;
            $this->host = $host;
            
            $this->res  = null;
            
            $this->error = array("number"=>"0","description"=>"");
            
            // Connect to host and select database
            $this->link = @mysql_connect($this->host,$this->user,$this->pass);
            if( !$this->link || mysql_errno() != 0) {
                $this->error["number"] = mysql_errno();
                $this->error["description"] = mysql_error();
                return $this->reportError();
            }
            
            if( !@mysql_select_db($this->db,$this->link) ) {
                $this->error["number"] = mysql_errno();
                $this->error["description"] = mysql_error();
                $this->reportError();
                return;
            }
        }
        
        /**
         * Use UTF-8 connection
         */
        public function useUTF8()
        {
        	$this->executeQuery("SET NAMES 'utf8'",$this->link);
        }
        
        /**
         * Escape strings
         * @param   $str    String to escape
         * @return  The escaped string
         */
        public function escapeString($str) {
            return mysql_real_escape_string($str,$this->link);
        }
        
        /**
         * Sends a query and returns the result
         * @param   $query  Query to execute
         * @param   $quiet  If true: errors won't be displayed
         * @return  Resource (result)
         */
        public function executeQuery($query,$quiet=false) {
            // Reset error array
            $this->error["number"] = "0";
            $this->error["description"] = "";
            
            // Execute query
            $this->res = mysql_query($query,$this->link);
            
            if( mysql_errno() != 0) {
                $this->error["number"] = mysql_errno();
                $this->error["description"] = mysql_error();
                // Report error?
                if( ! $quiet ) $this->reportError();
                return;
            }
            
            return $this->res;
        }
        
        /**
         * Returns the error array
         * @return  Errors (array)
         */
        public function getError() {
            return $this->error;
        }
        
        /**
         * Returns the current link identifier
         * @return  Connection id
         */
        public function getLink() {
            return $this->link;
        }
        
        /**
         * Returns the current otw. last result
         * @return  Ressource
         */
        public function getRessource() {
            return $this->res;
        }
        
        /**
         * Report an error
         */
        public function reportError() {
            echo '
                <div style="padding:10px;border: #FF0000 2px solid;background:#FFFFCC;font-family:sans-serif;font-size:12px">
                    <h3 style="color:#FF0000;">MySQL error</h3>
                    <p>
                        <b>Number:</b> '.$this->error["number"].'<br />
                        <b>Description:</b> '.$this->error["description"].'<br />
                    </p>
                </div>';
            exit(1);
                        
        }
        
    }
?>
